function getTopDeclStr()
{  
    var retStr= '';
    var dt = document.doctype;
	var htmlStr = document.documentElement.outerHTML;
    
    if (dt) {
        
        retStr += '<!DOCTYPE';
        
        if (dt.name.length) {
            retStr += ' ' + dt.name;
        }
        
        if (dt.publicId.length) {
            retStr += ' PUBLIC "' + dt.publicId + '"';
        }
        
        if (dt.systemId.length) {
            retStr += ' "' + dt.systemId + '"';
        }
        
        retStr += '>';
    }
	
	retStr += htmlStr.substring(0, htmlStr.indexOf("<head>"));
    
    return retStr;
}

/**
 * Print a div
 * @author trungnnh
 */
function printDiv(divId, cssUrlArr, preview)
{
	var $srcDiv = $("#" + divId);
	
	var wHeight = $(window).height();
	wHeight = (wHeight < 675) ? 675 : wHeight;
	
	var newWin = open(
		'',
		'_blank',
			'height=' + wHeight + ',' +
			'width=' + $srcDiv.width() + ',' +
			'top=0,' +
			'resizable=no,' + 
			'scrollbars=yes,' + 
			'location=no,' +
			'status=no,' + 
			'titlebar=no,' +
			'toolbar=no,' + 
			'menubar=yes'
	);

	var newDoc = newWin.document;
	
	newDoc.open();
	newDoc.write(getTopDeclStr());
	newDoc.write('<head>');
	newDoc.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
	
	// Xóa ?v=Math.random() khi release sản phẩm
	for (i = 0; i < cssUrlArr.length; i++) {
		newDoc.write('<link type="text/css" rel="stylesheet" href="' 
				+ cssUrlArr[i] 
				+ '?v=' + Math.random() 
				+ '">');
	}
	
	newDoc.write('</head><body');
	newDoc.write(' id="' + $srcDiv.prop("id") + '" class="' + $srcDiv.prop("class") + '"');
	newDoc.write(' style="border: 0 none;">');
	newDoc.write($srcDiv.html());
	
	if (!preview) {
		newDoc.write('<script type="text/javascript">');
		newDoc.write('print(); close();');
		newDoc.write('</script>');
	}
	
	newDoc.write('</body></html>');
	newDoc.close();
	newWin.focus();
}


$(document).ready(function() 
{
	var $ctn = $("#wysiwyp-ctn");
	var itemLineHeight = parseInt($( ".item" ).css("line-height"));

	$( ".item" )
		.draggable({
			containment: $ctn,
			scroll: false
		})
		.resizable({
			containment: $ctn,
			minHeight: itemLineHeight,
			minWidth: 100,
			grid: [ 1, itemLineHeight ]
		});
});